﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Xml;
using System.Web;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.Text;
using System.Data.SqlClient;
namespace Deeper.Common
{
	
	/// <summary>
	/// XmlFile 的摘要说明
	/// </summary>
	public static class XmlHelper
	{

		public static string ReadXMLValue(this System.Xml.XmlNode node, string path, System.Xml.XmlNamespaceManager nsm)
		{
			System.Xml.XmlNode node2 = node.SelectSingleNode(path, nsm);
			if (node2 == null)
				return null;
			else
			{
				if (node2 is System.Xml.XmlElement)
					return ((System.Xml.XmlElement)node2).InnerText;
				else
					return node2.Value;
			}
		}

		public static string ToXml<T>(this T obj)
		{
			MemoryStream ms = new MemoryStream();
			XmlSerializer xml = new XmlSerializer(obj.GetType());
			xml.Serialize(ms, obj);
			string result = Encoding.UTF8.GetString(ms.ToArray());
			ms.Close();
			return result;
		}

		public static T FromXml<T>(this string xml)
		{
			XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
			MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml));
			object obj = xmlSerializer.Deserialize(ms);
			return (T)obj;
		}
	}

}